import json
from datetime import date, datetime

result_message = {100:'ok',
                101:'request failed',
                102:'method not allowed',
                103:'parameter error',
                104:'username or password error',
                105:'server internal error',
                106:'timeout',
                107:'connection error',
                108:'verify expired',
                109:'no data'}

# return the json result
def result(code,result=[]):
    data = {'code':code,'message':result_message.get(code),'result':result}
    return json.dumps(data,cls=CJsonEncoder,sort_keys=True,ensure_ascii = False)


class CJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)


#
# class Result(object):
#     """docstring for Result"""
#
#     @staticmethod
#     def loadResult(self,code,result=[]):
#         data = {'code':code,'message':result_message.get(code),'result':result}
#         return json.dumps(data,sort_keys=True,ensure_ascii = False)
#
#     def dumpsResult(self,data):
#         d = json.load(data)
#         li = d['result']
